package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.PageResult;
import com.zzyl.nursing.domain.WxProjectQueryParam;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 *
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private static final String Nursing_PROJECT_KEY = "nursingProject:all";
    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        int insert = nursingProjectMapper.insert(nursingProject);
        //清除缓存
        deleteCache();
        return insert;
    }

    private void deleteCache() {
        redisTemplate.delete(Nursing_PROJECT_KEY);
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        int i = nursingProjectMapper.updateById(nursingProject);
        //清除缓存
        deleteCache();
        return i;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        int i = nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
        //清除缓存
        deleteCache();
        return i ;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        int i = nursingProjectMapper.deleteById(id);
        //清除缓存
        deleteCache();
        return i;
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        List<NursingProjectVo> list = (List<NursingProjectVo>)(redisTemplate.opsForValue().get(Nursing_PROJECT_KEY));

        if(list!=null && !list.isEmpty()){
            return list;
        }

        list = nursingProjectMapper.getAllProjects();

        redisTemplate.opsForValue().set(Nursing_PROJECT_KEY,list);

        return list;
    }


    /**
     * 分页查询护理项目列表（传给小程序端）
     */
    @Override
    public PageResult<NursingProject> PageProject(WxProjectQueryParam param) {
        //开启分页
        PageHelper.startPage(param.getPage(), param.getPageSize());

        //调mapper层
        List<NursingProject> list =nursingProjectMapper.selectProjectPage(param);
        Page<NursingProject> p = (Page<NursingProject>) list;
        return new PageResult<NursingProject>(p.getTotal(),p.getResult());
    }
}
